// 引入发送请求的工具
import API from '@/api'
// 解构获得自己封装的api
const { songlist } = API
// 时间
import moment from 'moment'

// 包含多个状态数据的对象
const state = {
  songlistArr: [], // 歌单标签数据
  playlist: [], // 歌单数据
  trackIdlist: {}, //歌单详情
  songTimeArr: [], // 歌曲时间
  albumList: {}, //专辑数据
  songsList: [] // 专辑歌曲
}
// 包含多个直接修改状态数据的方法(同步)
const mutations = {
  // 获取标签数据
  RECEIVE_SONG_LIST(state, songlistArr) {
    // 大数据冻结
    state.songlistArr = Object.freeze(songlistArr)
  },
  // 存储歌单数据
  RECEIVE_PLAY_LIST(state, playlist) {
    // 大数据冻结
    state.playlist = Object.freeze(playlist)
  },
  // 存储歌单详情
  RECEIVE_PLAY_ID(state, { playlist, songTimeArr }) {
    // 大数据冻结
    state.trackIdlist = Object.freeze(playlist)
    state.songTimeArr = songTimeArr
  },
  // 存储专辑详情
  RECEIVE_ALBUM_ID(state, { songs, album }) {
    state.songsList = songs
    state.albumList = album
  }
}
// 包含多个间接修改状态数据的方法(异步)
const actions = {
  // 获取标签数据
  async getSongList({ commit }) {
    //    console.log(reqSongList)
    const res = await songlist.reqSongList()
    // console.log(res);
    if (res.code === 200) {
      commit('RECEIVE_SONG_LIST', res.sub)
    }
  },
  // 获取歌单数据
  async getSongListConter({ commit }, { cat ,offset}) {
    const res = await songlist.reqSongListConter( cat,offset)
    // console.log(res);
    if (res.code === 200) {
      commit('RECEIVE_PLAY_LIST', res.playlists)
      // console.log(state.playlist)
    }
  },
  // 获取歌单详情
  async getTrackIdList({ commit }, songId) {
    const res = await songlist.reqPlayList(songId)
    // console.log(res);
    if (res.code === 200) {
      const { playlist } = res
      let songTimeArr = playlist.tracks.reduce((pre, item) => {
        pre.push(moment(item.dt).format('mm:ss'))
        return pre
      }, [])
      // console.log(songTimeArr)
      commit('RECEIVE_PLAY_ID', { playlist, songTimeArr })
      console.log(state.trackIdlist)
    }
  },
  // 获取专辑数据
  async getAlbumList({ commit }, albumId) {
    const res = await songlist.reqAlbumList(albumId)
    // console.log(res)
    if (res.code === 200) {
      const { songs, album } = res
    console.log(songs,album)

      commit('RECEIVE_ALBUM_ID', { songs, album })
      // console.log(state.playlist)
    }
  }
}

// 包含了多个状态数据计算属性的对象(只有get方法)
const getters = {}

// 模块名 === 创建的js文件名字
// state, // this.$store.state.模块名.键名
// actions, // this.$store.dispatch('模块名/getUsers', this.user)
// mutations, // this.$store.commit('模块名/getUsers', this.user)
// getters // this.$store.getters['模块名/demo']

export default {
  namespaced: true, // 开启命名空间
  state,
  actions,
  mutations,
  getters
}
